package com.mz.ozy.activity.me;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.FriendListAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.FriendBean;
import com.mz.ozy.bean.PronotionBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.FriendPresenter;
import com.mz.ozy.mvp.view.FriendContract;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author liucan on 2020/4/1 0001.
 */
public class FriendListActivity extends BaseActivity<FriendContract.View, FriendPresenter> implements FriendContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.iv_me_right)
    TextView ivMeRight;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.userIconIv)
    ImageView userIconIv;
    @BindView(R.id.tv_my_invite_num)
    TextView tvMyInviteNum;
    @BindView(R.id.tv_my_invite_gw)
    TextView tvMyInviteGw;
    private int pageNo = 1;
    private int tag = 0;
    private int type = 1;
    private List<FriendBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private FriendListAdapter mAdapter;
    private PronotionBean dataItem;
    private FriendBean data;
    private FriendBean friendBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend);
        ButterKnife.bind(this);
//        StatusBarUtil.setColor(FriendListActivity.this, getResources().getColor(R.color.c_73b21a), 100);
        QMUIStatusBarHelper.translucent(this);
        initTitle();
        setTitleBarMargin();
        tvBack.setImageResource(R.mipmap.white_back);
        tvCenterTitle.setText("好友列表");
        tvCenterTitle.setTypeface(Typeface.DEFAULT_BOLD);
        tvCenterTitle.setTextColor(ContextCompat.getColor(this, R.color.white));
        mPresenter.vipUpDataSh(this, "", type);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new FriendListAdapter(mBeanList, type);
        recyclerview.setAdapter(mAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getFriendInfo(FriendListActivity.this, pageNo + "");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getFriendInfo(FriendListActivity.this, pageNo + "");
            }
        });

        mPresenter.vipUpSh(this, type);
    }

    @OnClick({R.id.back, R.id.tv_updata})
    public void onViewClicked(View view) {
        try {
            if (friendBean != null || dataItem != null || data != null) {
                switch (view.getId()) {
                    case R.id.back:
                        finish();
                        break;
                    case R.id.tv_updata:
                        if (friendBean.getGrade() == 0 || data.getPromotion_map().getState() == 4) {
                            UpdataActivity.go(this, data.getPromotion_map().getId(), 0, true);
                        } else if (data.getPromotion_map().getState() == 2) {
                            startActivity(new Intent(FriendListActivity.this, PromotionTypeActivity.class));
                        } else if (data.getPromotion_map().getState() == 1 && dataItem.getIfup() == 1) {
                            UpdataActivity.go(this, data.getPromotion_map().getId(), 0, true);
                        } else {
                            startActivity(new Intent(FriendListActivity.this, PromotionTypeActivity.class));
                        }
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getFriendSuccess(FriendBean bean) {
        friendBean = bean;
        tvMyInviteNum.setText(bean.getNum() + "");
        tvMyInviteGw.setText(bean.getJknum() + "");
        tvUsername.setText(TextUtils.isEmpty(bean.getNickname()) ? SPUtils.getInstance(this).getString(Constants.SPName.USERNAME, "") : bean.getNickname());
        GlideUtils.loadAvatar(this, userIconIv, bean.getUseravatar());
        tvPhone.setText("ID: " + bean.getInvitecode());
//        mAdapter.setInvitecode(bean.getInvitecode());
        switch (bean.getGrade()) {
            case 0:
                ivMeRight.setText("普通会员");
                break;
            case 1:
                ivMeRight.setText("VIP");
                break;
            case 2:
                ivMeRight.setText("金卡");
                break;
            case 3:
                ivMeRight.setText("钻卡");
                break;
            case 4:
                ivMeRight.setText("健康顾问");
                break;
            case 5:
                ivMeRight.setText("资深顾问");
                break;
            case 6:
                ivMeRight.setText("蓝宝顾问");
                break;
            case 7:
                ivMeRight.setText("绿宝顾问");
                break;
            case 8:
                ivMeRight.setText("红宝督导");
                break;
            case 9:
                ivMeRight.setText("蓝钻督导");
                break;
            case 10:
                ivMeRight.setText("绿钻督导");
                break;
            case 11:
                ivMeRight.setText("红钻督导");
                break;
            case 12:
                ivMeRight.setText("黑钻首席");
                break;
        }
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void getVipUpSuccess(JSONObject bean) {

    }

    @Override
    public void getVipUpShSuccess(FriendBean bean) {
        data = bean;
    }

    @Override
    public void getVipUpDataInfoSuccess(PronotionBean bean) {
        dataItem = bean;
    }
}
